package test.core;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.FixMethodOrder;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import hyl.core.MyFun;
import hyl.core.run.IFilter;
import hyl.core.run.ISignalLamp;
import hyl.core.struct.MyMapreduce;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class testmapreduce {
	public static void main(String[] args) {
		testmapreduce tm = new testmapreduce();
		// tm.test();
		tm.test2();
		//tm.testleftjoin();
		//tm.testinnerjoin();
	}
	static List arr1 = null;
	static List arr2 = null;
	static List arr3 = null;
	Map obj = null;
	@Test
	public void test_a_1() {
		arr1 = new JSONArray();
		arr2 = new JSONArray();
		arr3 = new JSONArray();
		obj = new JSONObject();
		//List<Map> testarr=arr1;
		obj.put("name", "小红");
		obj.put("age", "18");
		obj.put("sex", "女");
		arr1.add(obj);
		obj = new JSONObject();
		obj.put("name", "小名");
		obj.put("age", "18");
		obj.put("sex", "男");
		arr1.add(obj);
		obj = new JSONObject();
		obj.put("name", "小红");
		obj.put("age", "19");
		obj.put("sex", "lv");
		obj.put("addr", "南通市");
		arr2.add(obj);
		obj = new JSONObject();
		obj.put("name", "小红");
		obj.put("语文", "98");
		obj.put("数学", "90");
		obj.put("addr", "南通市崇川区");
		arr3.add(obj);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void test2() {
		arr1 = new ArrayList();
		arr2 = new ArrayList();
		arr3 = new ArrayList();
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("age", "18");
		obj.put("sex", "女");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小名");
		obj.put("age", "18");
		obj.put("sex", "男");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("age", "19");
		obj.put("sex", "lv");
		obj.put("addr", "南通市");
		arr2.add(obj);
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("语文", "98");
		obj.put("数学", "90");
		obj.put("addr", "南通市崇川区");
		arr3.add(obj);
	}
	@Test
	public void test_a_2_leftjoin() {
		List li = (List<Map>) MyMapreduce.leftjoin(arr1, arr2, "name=name");
		System.out.println(JSON.toJSONString(li));
	}
	@Test
	public void test_a_3_innerjoin() {
		List li = (List<Map>) MyMapreduce.leftjoin(arr1, arr2, "name=name");
		li = MyMapreduce.innerjoin(li, arr3, "name=name");
		System.out.println(JSON.toJSONString(li));
	}
	@Test
	public void test1_addlist() {
		arr1 = new ArrayList();
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("月份", 7);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", 85.5);
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("月份", 8);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", "89");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("月份", 9);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", "98.5");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小红");
		obj.put("月份", 10);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", "80");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小蓝");
		obj.put("月份", 8);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", 99.5);
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小蓝");
		obj.put("月份", 9);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", "63");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小蓝");
		obj.put("月份", 10);
		obj.put("age", "18");
		obj.put("sex", "女");
		obj.put("语文", "89");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小黑");
		obj.put("月份", 10);
		obj.put("age", "19");
		obj.put("sex", "男");
		obj.put("语文", "100");
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小黑");
		obj.put("月份", 7);
		obj.put("age", "20");
		obj.put("sex", "男");
		obj.put("语文", 99.5);
		arr1.add(obj);
		obj = new HashMap();
		obj.put("name", "小黑");
		obj.put("月份", 8);
		obj.put("age", "20");
		obj.put("sex", "男");
		obj.put("语文", "95.5");
		arr1.add(obj);
	}
	@Test
	public void test2_groupby() {
		arr2 = MyMapreduce.groupby(arr1, "name", "min:语文,max:语文,sum:语文,count:,first:月份,last:月份");
		System.out.println("groupby name:" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.groupby(arr1, "月份", "min:语文,max:语文,sum:语文,count:");
		System.out.println("groupby 月份:" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.groupby(arr1, "", "min:语文,max:语文,sum:语文,count:,min:age");
		System.out.println("all groupby :" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.groupby(arr1, null, "min:语文,max:语文,sum:语文,count:,first:sex");
		System.out.println("all groupby :" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.groupby(arr1, "min:语文,max:语文,sum:语文,count:");
		System.out.println("all groupby :" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.groupby(arr1, null, "min:语文,max:语文,sum:语文,count:", new ISignalLamp<Map>() {
			@Override
			public int[] run(Map mp, int[] 参数) {
				if (mp.get("name").equals("小黑") && mp.get("age").equals("19"))
					参数[1] = 0;
				return 参数;
			}
		});
		System.out.println("条件 groupby :" + JSON.toJSONString(arr2));
	}
	@Test
	public void test3_orderby() {
		arr2 = MyMapreduce.orderby(arr1, "月份,语文");
		System.out.println("order月份,语文 :" + JSON.toJSONString(arr2));
	}
	@Test
	public void test4_where() {
		arr2 = MyMapreduce.where(arr1, new IFilter<Map>() {
			@Override
			public boolean where(Map obj) {
				if (MyFun.compareNum(obj.get("月份"),8)==0)
					return true;
				return false;
			}			
		});
		System.out.println("where 月份=8:" + JSON.toJSONString(arr2));
	}
	@Test
	public void test5_distinct() {
		arr2 = MyMapreduce.distinct(arr1, "name,月份");
		System.out.println("distinct name,月份:" + JSON.toJSONString(arr2));
	}
	@Test
	public void test6_select() {
		arr2 = MyMapreduce.select(arr1, "姓名=name,月份,年龄=age,语文");
		System.out.println("select 姓名=name,月份,性别=sex,年龄=age,语文:" + JSON.toJSONString(arr2));
		arr2 = MyMapreduce.select(arr1, "月份,月份=name,性别=sex,");
		System.out.println("select 月份=name,月份,性别=sex," + JSON.toJSONString(arr2));
	}
	@Test
	public void test7_merge() {
		HashMap obj = new HashMap();
		obj.put("name", "小纲");
		obj.put("sex", "男");
		obj.put("语文", "100");
		HashMap obj1 = new HashMap();
		obj1.put("name", "小纲");
		obj1.put("月份", 10);
		obj1.put("sex", "女");	
		obj1.put("语文", "89");
		HashMap obj2 = new HashMap();
		obj2.put("name", "小纲");
		obj2.put("月份", 8);
		obj2.put("age", "19");	
		obj2.put("语文", "99");
		Map obj3 = MyMapreduce.merge(obj,obj1,obj2);
		System.out.println("merge map...:" + JSON.toJSONString(obj3));
		
		obj3 = MyMapreduce.assign(obj,obj1,obj2);
		System.out.println("assign map...:" + JSON.toJSONString(obj3));
	}
}
